package org.jtao.gbsubject.service;

import org.jtao.gbsubject.domain.SubjectClazz;
import org.jtao.gbsubject.domain.TopSubject;
import org.jtao.gbsubject.repository.SubjectClazzRepository;
import org.jtao.gbsubject.repository.TopSubjectRepository;
import org.jtao.gbsubject.service.dto.SubjectClazzDTO;
import org.jtao.gbsubject.service.mapper.SubjectClazzMapper;
import org.jtao.gbsubject.service.mapper.TopSubjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Service Implementation for managing SubjectClazz.
 */
@Service
@Transactional
public class SubjectClazzService {

    private final Logger log = LoggerFactory.getLogger(SubjectClazzService.class);

    private final SubjectClazzRepository subjectClazzRepository;

    private final SubjectClazzMapper subjectClazzMapper;

    private final TopSubjectRepository topSubjectRepository;

    private final TopSubjectMapper topSubjectMapper;

    private final TopSubjectService topSubjectService;

    public SubjectClazzService(SubjectClazzRepository subjectClazzRepository, SubjectClazzMapper subjectClazzMapper, TopSubjectRepository topSubjectRepository, TopSubjectMapper topSubjectMapper, TopSubjectService topSubjectService) {
        this.subjectClazzRepository = subjectClazzRepository;
        this.subjectClazzMapper = subjectClazzMapper;
        this.topSubjectRepository = topSubjectRepository;
        this.topSubjectMapper = topSubjectMapper;
        this.topSubjectService = topSubjectService;
    }

    /**
     * Save a subjectClazz.
     *
     * @param subjectClazzDTO the entity to save
     * @return the persisted entity
     */
    public SubjectClazzDTO save(SubjectClazzDTO subjectClazzDTO) {
        log.debug("Request to save SubjectClazz : {}", subjectClazzDTO);
        SubjectClazz subjectClazz = subjectClazzMapper.toEntity(subjectClazzDTO);
        subjectClazz = subjectClazzRepository.save(subjectClazz);
        return subjectClazzMapper.toDto(subjectClazz);
    }

    /**
     * Get all the subjectClazzes.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<SubjectClazzDTO> findAll(Pageable pageable) {
        log.debug("Request to get all SubjectClazzes");
        return subjectClazzRepository.findAll(pageable)
            .map(subjectClazzMapper::toDto);
    }

    /**
     * Get one subjectClazz by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public SubjectClazzDTO findOne(Long id) {
        log.debug("Request to get SubjectClazz : {}", id);
        SubjectClazz subjectClazz = subjectClazzRepository.findOne(id);
        return subjectClazzMapper.toDto(subjectClazz);
    }

    /**
     * Delete the subjectClazz by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete SubjectClazz : {}", id);
        subjectClazzRepository.delete(id);
    }

    public Integer importSubjectClazz(MultipartFile file) {
        List<SubjectClazz> subjectClazzList = ImportService.importExcel(file, 1, 1, SubjectClazz.class);
        int sum = 0;
        for (int i = 0; i < subjectClazzList.size(); i++) {
            SubjectClazz subjectClazzExcel = subjectClazzList.get(i);
            SubjectClazz subjectClazzNew = new SubjectClazz();
            //判断表格中的专业类名称是否为空，如果非空则判断在数据库中是否存在此名称。
            if (subjectClazzExcel.getName() != null) {
                String subjectClazzName = subjectClazzExcel.getName();
                subjectClazzNew.setName(subjectClazzName);
                String topSubjectName = subjectClazzExcel.getTopSubject().getName();
                //判断学科门类是否为空，如果非空则判断在数据库中是否存在此学科门类。
                if (topSubjectName != null) {
                    TopSubject topSubject1 = topSubjectRepository.findOneByName(topSubjectName);
                    //数据库中不存在此学科门类==>新建并持久化数据。
                    if (topSubject1 == null) {
                        TopSubject topSubject = new TopSubject();
                        topSubject.setName(topSubjectName);
                        topSubjectService.save(topSubjectMapper.toDto(topSubject));
                        subjectClazzNew.setTopSubject(topSubject);
                    }
                    //数据库中存在此学科门类==>选择该学科门类。
                    else {
                        subjectClazzNew.setTopSubject(topSubject1);
                    }
                }
                SubjectClazz subjectClazzDB = subjectClazzRepository.findOneByName(subjectClazzExcel.getName());
                //数据库中不存在此专业类==>新建并持久化数据。
                if (subjectClazzDB == null) {
                    sum++;
                    this.save(subjectClazzMapper.toDto(subjectClazzNew));
                } else {
                    //数据库中存在，则进行更新
                    subjectClazzNew.setId(subjectClazzDB.getId());
                    this.save(subjectClazzMapper.toDto(subjectClazzNew));
                }
            }
        }
        System.out.println("导入数据一共【" + sum + "】行");
        return sum;
    }
}
